/*
 * @Autor: 黄旗
 * @Date: 2020-06-11 15:55:48
 */
// Vue
import Vue from 'vue';
import App from './App';
import VCharts from 'v-charts';
// websocket
import * as $websocket from './plugins/request/chatwebsocket';
// import util from '@/libs/util';

// 配置
import Setting from './setting';

// 混合
import mixinApp from '@/mixins/app';

// 插件
import plugins from '@/plugins';

// store
import store from '@/store/index';

// iView 和 iView Pro
import ViewUI from 'view-design';
import iViewPro from '@/libs/iview-pro/iview-pro.min.js';

// 菜单和路由
import router from './router';
// import menuHeader from "@/menu/header";
// import menuSider from "@/menu/sider";
import { frameInRoutes } from '@/router/routes';
Vue.prototype.$avatar = require('../src/assets/images/avaters.png')

// 多语言
import i18n from '@/i18n';

// 方法
import { getHeaderName, getMenuSider, getSiderSubmenu} from '@/libs/system';

// 内置组件
import iLink from '@/components/link';
import jgcheckcity from "@/components/checkcity";
import './directives/index.js'; // 自定义指令按钮

// css样式
import './styles/qqface.less';

// 使用样式，修改主题可以在 styles 目录下创建新的主题包并修改 iView 默认的 less 变量
// 参考 https://www.iviewui.com/docs/guide/theme
import './styles/index.less';
import './libs/iview-pro/iview-pro.css';
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { UTableColumn, UTable, UxGrid, UxTableColumn } from 'umy-ui';
import log from './libs/Debug.js';

import storage from './libs/storage.js';

import { changeday, inputday, blurday } from './libs/day.js';
Vue.prototype.$websocket = $websocket;
Vue.use(UTableColumn);
Vue.use(UTable);
Vue.use(UxGrid);
Vue.use(UxTableColumn); // 开发打印
Vue.prototype.logs = log; // 开发打印
Vue.prototype.storage = storage; // 时间处理
Vue.prototype.$changeday = changeday;
Vue.prototype.$inputday = inputday;
Vue.prototype.$blurday = blurday;
// 虚拟列表
Vue.prototype.$bus=new Vue()
Vue.prototype.$Setting = Setting;
if (window) window.$t = (key, value) => {
    return i18n.t(key, value);
};
Vue.use(VCharts);

Vue.use(plugins);
Vue.use(ViewUI, {
    transfer: true,
    i18n: (key, value) => {
        return i18n.t(key, value);
    }
});
Vue.use(iViewPro);
Vue.component('i-link', iLink);
Vue.component('jgcheckcity', jgcheckcity);
Vue.filter('rateFilter', function (value) {
    return parseInt(value * 100) + '%';
});
import defaultImg from '@/assets/images/erroimg.jpg';
import loadingImg from '@/assets/images/loading.gif';
import VueLazyload from 'vue-lazyload'; // 引入这个懒加载插件
// Vue.use(VueLazyload) // 直接使用
Vue.use(VueLazyload, { // 添加自定义选项
    preLoad: 1.3,
    error: defaultImg, // 加载错误时候的图片
    loading: loadingImg, // 加载中的图片
    attempt: 7,
    listenEvents: ['scroll']
});

new Vue({
    mixins: [mixinApp],
    router,
    store,
    i18n,
    render: h => {
        return h(App);
    },
    created() {
        // 处理路由 得到每一级的路由设置
        this.$store.commit('admin/page/init', frameInRoutes);
        // 设置顶栏菜单
        let menus = JSON.parse(localStorage.getItem('menus'));
        // let project = JSON.parse(localStorage.getItem('project'));
        // let currentProject = JSON.parse(localStorage.getItem('currentProject'));
        // console.log(this.$route,'route')
        // if(!project||!currentProject){
        //     router.push({
        //         name: 'login',
        //     });
        // }
        if (menus) {
            this.$store.commit('admin/menu/setHeader', menus.header);
            // }else{
            //     router.push({
            //         name: 'login',
            //     });
        }
        // 加载用户登录的数据
        this.$store.dispatch('admin/account/load');
        // 初始化全屏监听
        this.$store.dispatch('admin/layout/listenFullscreen');
    },
    watch: {
        // 监听路由 控制侧边栏显示 标记当前顶栏菜单（如需要）
        $route(to, from) {
            // console.log(from, 'from')
            // console.log(to, 'to')
            let project = JSON.parse(localStorage.getItem('project'));
            let currentProject = JSON.parse(localStorage.getItem('currentProject'));
            let menus = JSON.parse(localStorage.getItem('menus'));


            if (!project || !currentProject || !menus) { // 项目or菜单存在                
                if(to.path=='/clueform/'){
                    this.appRouteChange(to, from);
                }else if (from.path !== '/login' && to.path !== '/login') {
                    router.push({
                        name: 'login',
                    });
                } else {
                    this.appRouteChange(to, from);
                }
            } else {
                const path = to.path;
                let menus;
                menus = JSON.parse(localStorage.getItem('menus'));
                // console.log(menus,to.path)
                const headerName = getHeaderName(path, menus.sider);
                let preHeadName = getHeaderName(from.path, menus.sider);
                this.$store.commit('admin/menu/setHeaderName', headerName);
                let filterMenuSider;
                if (to.path === '/business/userSetting') {
                    filterMenuSider = getMenuSider(menus.sider, preHeadName);
                } else {
                    filterMenuSider = getMenuSider(menus.sider, headerName);
                }
               
                this.$store.commit('admin/menu/setSider', filterMenuSider);
                this.$store.commit('admin/menu/setActivePath', path);
                const openNames = getSiderSubmenu(path, menus.sider);
                this.$store.commit('admin/menu/setOpenNames', openNames);
                // this.$store.commit("admin/menu/setActivePath", path);
                
                this.appRouteChange(to, from);                
            }
        }
    },
    mounted() {
        window.addEventListener('beforeunload', e => {
            // console.log(333333);
        });
    }
}).$mount('#app');
